/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.mes.opcua.controller;

import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.jackson.JsonUtil;
import org.springblade.modules.mes.opcua.constant.OpcUaKey;
import org.springblade.modules.mes.opcua.itf.OpcUa;
import org.springblade.modules.mes.opcua.itf.mock.MockOpcUa;
import org.springblade.modules.mes.opcua.vo.MockVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 生产站位 控制器
 *
 * @author BladeX
 * @since 2020-09-10
 */
@RestController
@AllArgsConstructor
@RequestMapping("/mock")
@Api(value = "生产站位", tags = "生产站位接口")
public class MockController extends BladeController {

	private final MockOpcUa mockOpcUa;


	@GetMapping("/test_request")
	public Map<String, Object> testRequest(MockVO vo) {
		mockOpcUa.getDataMap().clear();
		mockOpcUa.fireEvent(vo.getPre() + OpcUaKey.orderStatus + vo.getPos(), 1);
		waitFor();
		return mockOpcUa.getDataMap();
	}

	@GetMapping("/test_archive")
	public Map<String, Object> test2(MockVO vo) {
		mockOpcUa.getDataMap().clear();
		mockOpcUa.fireEvent(vo.getPre() + OpcUaKey.archive, 1);
		waitFor();
		return mockOpcUa.getDataMap();
	}

	@GetMapping("/test_scan")
	public Map<String, Object> test3(MockVO vo) {
		mockOpcUa.getDataMap().clear();
		mockOpcUa.getDataMap().put(vo.getPre() + OpcUaKey.scanCode, vo.getValue());
		mockOpcUa.fireEvent(vo.getPre() + OpcUaKey.scanStatus, 1);
		waitFor();
		return mockOpcUa.getDataMap();
	}

	@GetMapping("/test_param")
	public Map<String, Object> test4(MockVO vo) {
		mockOpcUa.getDataMap().clear();
		String[] vs = vo.getValue().split(",");
		for (int i = 0; i < vs.length; i++) {
			mockOpcUa.getDataMap().put(vo.getPre() + OpcUaKey.param + i + vo.getPos(), vs[i]);
		}

		mockOpcUa.fireEvent(vo.getPre() + OpcUaKey.paramStatus+ vo.getPos(), 1);
		waitFor();
		return mockOpcUa.getDataMap();
	}


	private void waitFor() {
		for (int i = 0; i < 10000; i++) {
			sleep2(100);
			for (String key : mockOpcUa.getDataMap().keySet()) {
				if (key.endsWith(".act")) {
					Object value = mockOpcUa.getDataMap().get(key);
					if ("1".equals(value.toString())) {
						print();
						return;
					}
				}
			}
		}

	}

	private void sleep2(int t) {
		try {
			Thread.sleep(t);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private void print() {
		System.out.println("PLC***********************************************************************");
		for (String key : mockOpcUa.getDataMap().keySet()) {
			System.out.println(key + " : " + mockOpcUa.getDataMap().get(key));
		}
		System.out.println("PLC***********************************************************************");
	}


}
